package com.ff.compare.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ff.compare.enity.DataFromDB;
import com.ff.compare.mapper.CompareMapper;
import com.ff.compare.service.CompareService;
@Service
public class CompareServiceImpl implements CompareService{
	
	@Autowired
	private CompareMapper compareMapper;

	@Override
	public List<String> compareByExcel(List<String> productNameList) {
		List<String> errorProductNameList = new ArrayList<>();
		for(String productName:productNameList){
			//根据商品名数据库查询对应的该商品最高、最低成本单价
			DataFromDB dataFromDB = compareMapper.findMaxAndMinByName(productName);
			//若该商品名称在库内无信息则继续遍历
			if(StringUtils.isEmpty(dataFromDB)){
				continue;
			}
			BigDecimal max = new BigDecimal(dataFromDB.getMaxCost().toString());
			BigDecimal min = new BigDecimal(dataFromDB.getMinCost().toString());
			BigDecimal spread = new BigDecimal(max.subtract(min).toString());
			if(spread.divide(max, 2,BigDecimal.ROUND_HALF_UP).doubleValue()>0.2){
				errorProductNameList.add(productName);
			}
		}
		return errorProductNameList;
	}

}
